package com.lc.recovery.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.model.HttpData;
import com.lc.recovery.http.request.ForgetApi;
import com.lc.recovery.http.request.GetCodeApi;
import com.lc.recovery.http.response.PublicMsg;
import com.lc.widget.view.CountdownView;
import com.lc.widget.view.PasswordEditText;
import com.lc.widget.view.RegexEditText;

import androidx.appcompat.widget.AppCompatButton;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/02/27
 * desc   : 忘记密码
 */
public final class PasswordForgetActivity extends AppActivity {


    @BindView(R.id.et_password_forget_phone)
    RegexEditText etPasswordForgetPhone;
    @BindView(R.id.et_forget_code)
    RegexEditText etForgetCode;
    @BindView(R.id.et_password_forget_newphone)
    PasswordEditText etPasswordForgetNewphone;
    @BindView(R.id.et_password_forget_truephone)
    PasswordEditText etPasswordForgetTruephone;
    @BindView(R.id.btn_password_forget_commit)
    AppCompatButton btnPasswordForgetCommit;
    @BindView(R.id.cv_phone_reset_countdown)
    CountdownView cvPhoneResetCountdown;

    @Override
    protected int getLayoutId() {
        return R.layout.password_forget_activity;
    }

    @Override
    protected void initView() {


    }

    @Override
    protected void initData() {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.cv_phone_reset_countdown, R.id.btn_password_forget_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cv_phone_reset_countdown:

                if (etPasswordForgetPhone.getText().toString().length() != 11) {
                    etPasswordForgetPhone.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                    toast(R.string.common_phone_input_error);
                    return;
                }


                // 隐藏软键盘
                hideKeyboard(getCurrentFocus());

                // 获取验证码
                EasyHttp.post(this)
                        .api(new GetCodeApi()
                                .setMobile(etPasswordForgetPhone.getText().toString()).setType("2"))
                        .request(new HttpCallback<HttpData<Void>>(this) {

                            @Override
                            public void onSucceed(HttpData<Void> data) {
                                toast(R.string.common_code_send_hint);
                                cvPhoneResetCountdown.start();
                            }
                        });
                break;
            case R.id.btn_password_forget_commit:
                if (etPasswordForgetPhone.getText().toString().isEmpty()){
                    toast("电话号不能为空");
                    return;
                }
                if (etForgetCode.getText().toString().isEmpty()){
                    toast("验证码不能为空");
                    return;
                }
                if (etPasswordForgetNewphone.getText().toString().isEmpty()){
                    toast("新密码不能为空");
                    return;
                }
                if (etPasswordForgetNewphone.getText().toString().length()<6||etPasswordForgetNewphone.getText().toString().length()>20||etPasswordForgetTruephone.getText().toString().length()<6||etPasswordForgetTruephone.getText().toString().length()>20){
                    toast("密码需要6-20个字符");
                    return;
                }
                if (etPasswordForgetTruephone.getText().toString().isEmpty()){
                    toast("确认密码不能为空");
                    return;
                }
                if (!etPasswordForgetTruephone.getText().toString().equals(etPasswordForgetNewphone.getText().toString())){
                    toast("两次输入密码不一致");
                    return;
                }
                forget();

                break;
        }
    }

    private void forget() {
        EasyHttp.post(this)
                .api(new ForgetApi()
                        .setMobile(etPasswordForgetPhone.getText().toString()).setCode(etForgetCode.getText().toString()).setPassword(etPasswordForgetNewphone.getText().toString()))
                .request(new HttpCallback<PublicMsg>(this) {

                    @Override
                    public void onSucceed(PublicMsg data) {
                       if (data.getCode()==200){
                           toast(data.getMsg());
                           finish();
                       }else {
                           toast(data.getMsg());
                       }
                    }
                });
    }
}